//业务层
let movieDao = require("../Dao/movieDao.js");
let resultUtil = require("../util/ResultUtil.js");

//分页显示
async function hotList(filter, page = 1, limit = 10) {
    //中间工具
    let data = await movieDao.getHots(filter, page, limit);
    let count = await movieDao.hotsCount(filter);
    let rest = resultUtil(data, count);//？
    return rest;
}

//新增
async function addMovies(movie) {

    // 强行赋值
   /* movie.director = [
        {
            name: movie.directorName,
            image: movie.image,
        }
    ];
    movie.actor = [
        {
            name: movie.actorName,
            filmName: movie.filmName,
            image: movie.image,
        }
    ];
    movie.images = [
        { image: movie.image, }
    ]
    */


    let data = await movieDao.addMovie(movie);
    if (data.name) {
        return { code: 200, msg: "添加成功" }

    } else {
        return {
            code: -1, msg: "添加错误"
        }

    }
}

//删除
async function deletMovie(movieId) {
    let data = await movieDao.deleteMovie(movieId);
    if (data.deleteMovie > 0) {
        return {
            code: 200, msg: "删除成功"
        };
    } else {
        return {
            code: -1, msg: "删除成功"
        }
    }
}

//查询
async function getById(movieId) {
    let data = await movieDao.getMovies(movieId)
    return data;
}

//修改书
async function updateById(movie) {
    let data = await movieDao.updateById(movie);
    if (data.movieCount > 0) {
        return { code: 200, msg: "修改成功" }
    } else {
        return { code: -1, msg: "修改失败" }
    }
}
module.exports.updateById = updateById;
module.exports.getById = getById;
module.exports.deletMovie = deletMovie;
module.exports.addMovies = addMovies;
module.exports.hotList = hotList;